package com.zhonghui.controller;

import cn.hutool.core.util.NumberUtil;
import com.eshop.apiCode.EshopException;
import com.zhonghui.common.bean.LocalUser;
import com.zhonghui.common.eshopUtil.ApiResult;
import com.zhonghui.modlules.product.param.StoreProductRelationQueryParam;
import com.zhonghui.modules.service.ProductRelationService;
import com.zhonghui.modules.vo.StoreProductRelationQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(value = "产品模块",tags = "商城:产品模块")
public class ProductCollectController {

    private final ProductRelationService relationService;

    @ApiOperation(value = "添加收藏",notes = "添加收藏")
    @PostMapping("/collect/add")
    public ApiResult<Boolean> collectAdd(@Validated @RequestBody StoreProductRelationQueryParam param){
        Long uid = LocalUser.getUser().getUid();
        if (!NumberUtil.isNumber(param.getId())){
            throw new EshopException("参数有误");
        }
        relationService.addCollect(Long.valueOf(param.getId()),uid,param.getCategory());
        return ApiResult.ok();
    }

    @ApiOperation(value = "取消收藏",notes = "取消收藏")
    @PostMapping("/collect/del")
    public ApiResult<Boolean> collectDel(@Validated @RequestBody StoreProductRelationQueryParam param){
        Long uid = LocalUser.getUser().getUid();
        if (!NumberUtil.isNumber(param.getId())){
            throw new EshopException("参数有误");
        }
        relationService.quXiaoCollect(Long.valueOf(param.getId()),uid,param.getCategory());
        return ApiResult.ok();
    }

    @ApiOperation(value = "批量取消收藏",notes = "批量取消收藏")
    @PostMapping("/collect/dels/{productId}")
    public ApiResult<Boolean> collectDels(@Validated @RequestBody StoreProductRelationQueryParam param, @PathVariable String productId){
        Long uid = LocalUser.getUser().getUid();
        String[] ids = productId.split(",");
        if (ids.length > 0){
            for (String id : ids) {
                relationService.quXiaoCollect(Long.parseLong(id),uid,param.getCategory());
            }
        }else {
            throw new EshopException("参数非法");
        }
        return ApiResult.ok();
    }

    @ApiOperation(value = "获取收藏产品，或足迹",notes = "获取收藏产品，或足迹")
    @GetMapping("/collect/user/list")
    public ApiResult<List<StoreProductRelationQueryVo>> collectUser(@RequestParam(value = "page",defaultValue = "1")int page,
                                                                    @RequestParam(value = "limit",defaultValue = "500")int limit,
                                                                    @RequestParam(value = "type")String type){
        Long uid = LocalUser.getUser().getUid();
        List<StoreProductRelationQueryVo> relationQueryVoList = relationService.userCollectList(page, limit, uid, type);
        return ApiResult.ok(relationQueryVoList);
    }
}
